package ThreadDemo;

public class BlockingQueue {
    private int[] array=new int[1000];
    private volatile int head=0;
    private volatile int tail=0;
    //head和tail构成一个前闭后开区间
    private volatile int size=0;
    public void put(int value) throws InterruptedException {
        synchronized (this){
            while(size==array.length){
                wait();
            }
            array[tail]=value;
            tail++;
            if(tail==array.length){
                tail=0;
            }
            size++;
            notify();
        }
    }
    public int take() throws InterruptedException {
        int ret=-1;
        synchronized(this){
            while(size==0){
                this.wait();
            }
            ret=array[head];
            head++;
            if(head==array.length){
                head=0;
            }
            size--;
            notify();
        }
        return ret;
    }

    public static void main(String[] args) {
        BlockingQueue blockingQueue=new BlockingQueue();
        Thread producer = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    try {
                        blockingQueue.put(i);
                        System.out.println("生产元素："+i);
                        sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        };
        producer.start();
        Thread consumer = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    try {
                        int ret = blockingQueue.take();
                        System.out.println("消费元素:" + ret);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        consumer.start();
    }
}
